package demo;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

public class Xc {


    //一个线程还可以等待另一个线程直到其运行结束。例如，main线程在启动t线程后，可以通过t.join()等待t线程结束后再继续运行：
//    public static void main(String[] args) throws InterruptedException {
//        Thread thread = new Thread(()->{
//            System.out.println("hello");
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//        System.out.println("start");
//        thread.start();
//
//        thread.join();
//        System.out.println("end");
//    }

//    public static void main(String[] args) {
//        Thread t = new MyThread();
//        t.start();
//    }
//    public static void main(String[] args) {
//        Thread thread = new Thread(()->{
//            System.out.println(123456);
//        });
//        Thread thread1 = new Thread(new MyRunnable());
//        thread1.start();
//    }


    /**
     * main start...
     * main end...
     * thread run...
     * thread end.
     * @param args
     */
//    public static void main(String[] args) {
//        System.out.println("main start...");
//        Thread t = new Thread() {
//            public void run() {
//                System.out.println("thread run...");
//                System.out.println("thread end.");
//            }
//        };
//        t.start();
//        System.out.println("main end...");
//    }
//


    /**
     * main start...
     * thread run...
     * thread end.
     * main end...
     * @param args
     */
//    public static void main(String[] args) {
//        System.out.println("main start...");
//        Thread t = new Thread() {
//            public void run() {
//                System.out.println("thread run...");
//                try {
//                    Thread.sleep(10);
//                } catch (InterruptedException e) {}
//                System.out.println("thread end.");
//            }
//        };
//        t.start();
//        try {
//            Thread.sleep(20);
//        } catch (InterruptedException e) {}
//        System.out.println("main end...");
//    }

        public static void main(String[] args) throws InterruptedException {
            Thread t = new MyThread();
            t.start();
            Thread.sleep(1000);
            t.interrupt(); // 中断t线程
            t.join(); // 等待t线程结束
            System.out.println("end");
        }
    }

    class MyThread extends Thread {
        public void run() {
            Thread hello = new HelloThread();
            hello.start(); // 启动hello线程
            try {
                hello.join(); // 等待hello线程结束
            } catch (InterruptedException e) {
                System.out.println("interrupted!");
            }
            hello.interrupt();
        }
    }

    class HelloThread extends Thread {
        public void run() {
            int n = 0;
            while (!isInterrupted()) {
                n++;
                System.out.println(n + " hello!");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }

//class MyThread extends Thread{
//    @Override
//    public void run(){
//        System.out.println("start new thread");
//    }
//}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("实现runable接口");
    }
}
